package cn.it.hbtt.controller;

import cn.it.hbtt.entity.Student;
import cn.it.hbtt.entity.UserList;
import org.springframework.stereotype.Controller;
import cn.it.hbtt.entity.User;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.InternalResourceView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

@Controller
@SessionAttributes(value = {"username","password"},types = {String.class,Integer.class})//会把放入request域中的对象放入到session中
public class getPOJO {

    @RequestMapping("/getAddJSP")//获取提交表单的页面
    public String get(){
        return "index";
    }

    /**
     *
     *
     * @RequestMapping("/addUser")//处理页面表单提交的请求
     *     public String getPOJO(User user){
     *         System.out.println(user);
     *         return "add";
     *     }
     */
    @RequestMapping("/addUser")//处理页面表单提交的请求
    @ResponseBody
    public String getPOJO(User user){
        System.out.println(user);
        return "user:"+user;
    }

    @RequestMapping("/baseType")
    @ResponseBody
    public String baseType(int id){
        return "id"+id;
    }

    @RequestMapping("/packageType")
    @ResponseBody
    public String packageType(@RequestParam(required = true,defaultValue = "45") Integer id){
        return "id"+id;
    }


    @RequestMapping("/arrayType")
    @ResponseBody
    public String arrayType(String[] name){
        StringBuffer sbf = new StringBuffer();
        for (String item:name){
            sbf.append(item).append(" ");
        }
        return "name:"+sbf.toString();
    }

    @RequestMapping("/getlistType")
    public String getlistType(UserList userList){
        return "addUsers";
    }

    @RequestMapping("/listType")
    @ResponseBody
    public String listType(UserList userList) {
        StringBuffer sbf = new StringBuffer();
        for (User user : userList.getUsers()) {
            sbf.append(user);
        }
        //System.out.println(userList);
        return sbf.toString();

    }

    @RequestMapping(value = "/ajax")
    public String jsonTest(){
        return "jsonTest";
    }

    @RequestMapping(value = "/jsonType")
    @ResponseBody
    public User jsonType(@RequestBody User user){
        //修改年龄
        user.setAge(user.getAge()+10);
        //返回前端
        return user;
    }




    @RequestMapping("/mapTest")
    public String mapTest(Map<String,Object> map){
        User user = new User();
        user.setId(1);
        user.setName("张三");
        map.put("user",user);
        return "mapTest";
    }

    @RequestMapping("/modelTest")
    public String modelTest(Model model){
        User user = new User();
        user.setId(1);
        user.setName("张三");
        model.addAttribute("user",user);
        return "mapTest";
    }


    @RequestMapping("/modelAndViewTest1")
    public ModelAndView modelAndViewTest1(){
        ModelAndView modelAndView = new ModelAndView();
        User user = new User();
        user.setId(1);
        user.setName("李四");
        modelAndView.addObject("user",user);
        modelAndView.setViewName("mapTest");
        return modelAndView;
    }
    @RequestMapping("/modelAndViewTest2")
    public ModelAndView modelAndViewTest2(){
        ModelAndView modelAndView = new ModelAndView();
        User user = new User();
        user.setId(1);
        user.setName("李四");
        modelAndView.addObject("user",user);
        InternalResourceView internalResourceView = new InternalResourceView("/mapTest.jsp");
        modelAndView.setView(internalResourceView);
        return modelAndView;
    }


    @RequestMapping("requestTest")
    public String requestTest(HttpServletRequest request){
        User user = new User();
        user.setId(1);
        user.setName("wangwu");
        request.setAttribute("user",user);
        return "mapTest";
    }


    @RequestMapping("/modelAttributeTest")
    public String modelAttributeTest(){
        return "mapTest";
    }
    @ModelAttribute
    public User getUser(){
        User user = new User();
        user.setId(1);
        user.setName("wang");
        return user;
    }



    @RequestMapping("/testPut")
    public String testPut(Model model){
        model.addAttribute("username","张三");
        model.addAttribute("password","123456");
        model.addAttribute("age",20);
        return "success";
    }

    @RequestMapping("/testGet")
    public String testGet(ModelMap model){
        System.out.println(model.get("username")+";"+model.get("password")+";"+model.get("age"));
        return "success";
    }

    @RequestMapping("/testClean")
    //SessionStatus 用于移除Session中的数据
    public String complete(SessionStatus sessionStatus){
        sessionStatus.setComplete();
        return "success";
    }


    @RequestMapping(value = "/getaddDate")
    public String getaddDate(Date date){
        return "addDate";
    }

    @RequestMapping(value = "/dateConverterTest")
    @ResponseBody
    public String dateConverterTest(Date date){
        return date.toString();
    }


    @RequestMapping(value = "/getaddStudent")
    public String getaddStudent(Student student){
        return "addStudent";
    }

    @RequestMapping(value = "/studentConverterTest")
    @ResponseBody
    public String studentConverterTest(Student student){
        return student.toString();
    }

}
